package com.xinchuang.service.targetSystem.Impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.mapper.targetSystem.SystemTypeMapper;
import com.xinchuang.mapper.targetSystem.TargetTypeMapper;
import com.xinchuang.mapper.targetSystem.weapon.WeaponHouseMapper;
import com.xinchuang.entity.targetSystem.system.TargetType;
import com.xinchuang.entity.targetSystem.target.SystemType;
import com.xinchuang.entity.targetSystem.weapon.WeaponRelation;
import com.xinchuang.service.targetSystem.KnowService;
import com.xinchuang.service.targetSystem.SystemTypeService;
import com.xinchuang.util.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SystemServiceImpl
 * @Description: TODO(写一段描述的话)
 * @Author zhangxuan
 * @Date 2022-03-23 17:14
 * @Verion V1.0
 */
@Service
public class SystemTypeServiceImpl implements SystemTypeService {

    @Resource
    private SystemTypeMapper systemTypeMapper;

    @Resource
    private TargetTypeMapper targetTypeMapper;

    @Resource
    private WeaponHouseMapper weaponHouseMapper;

    @Resource
    private KnowService knowService;

    private SnowflakeIdWorker snow = new SnowflakeIdWorker(1, 1);

    @Override
    public Result selectTargetTypePut() {
        List<TargetType> types = targetTypeMapper.findTargetTypeByParantId("1");
        List<Map<String, Object>> resultList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(types)) {
            for (TargetType targetType : types) {
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("value", targetType.getTypeId());
                hashMap.put("label", targetType.getTypeName());
                resultList.add(hashMap);
            }
        }
        return ResultUtil.success(resultList);
    }

    @Override
    public Result addOrUpdateSystemType(SystemType systemType) {
        if (StringUtils.isBlank(systemType.getTypeId())) {
            return ResultUtil.error(404, "请选择系统类型");
        }
        if (null == systemType.getStatus()) {
            return ResultUtil.error(404, "请选择系统类型");
        }
        if (systemType.getStatus() == 1) {
            TargetType type = targetTypeMapper.selectTypeOnly(systemType.getTypeId());
            if (null != type && StringUtils.isNotBlank(type.getTypeName())) {
                systemType.setTypeName(type.getTypeName());
            }
        }
        if (StringUtils.isBlank(systemType.getId()) || systemType.getId().equals("0")) {
            if (systemType.getStatus() == 3 && systemTypeMapper.selectCountByTypeName(systemType.getTypeName(), systemType.getStatus()) > 0) {
                return ResultUtil.error(404, "类型名称已存在，请重新修改");
            } else {
                String id = systemTypeMapper.selectIdByName(systemType.getTypeId(), systemType.getStatus());
                if (systemType.getStatus() != 3) {
                    if (null != id) {
                        return ResultUtil.error(404, "类型名称已存在，请重新修改");
                    }
                }
            }
            systemType.setId(snow.nextId() + "");
            systemTypeMapper.addSystemTypeAndId(systemType);
            return ResultUtil.success("新增类型成功", null);
        } else {
            if (systemType.getStatus() != 3) {
                String id = systemTypeMapper.selectIdByName(systemType.getTypeId(), systemType.getStatus());
                if (null != id && !id.equals(systemType.getId())) {
                    return ResultUtil.error(404, "类型名称已存在，请重新修改");
                }
            } else {
                if (systemTypeMapper.selectCountNoNameByTypeName(systemType.getTypeName(), systemType.getStatus()) > 0) {
                    return ResultUtil.error(404, "类型名称已存在，请重新修改");
                }
                weaponHouseMapper.delWeaponMessageByRelationId(systemType.getId());
            }
            systemTypeMapper.addSystemTypeAndId(systemType);
            return ResultUtil.success("修改类型成功", null);
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Result deleteSystemType(String id) {
        SystemType type = systemTypeMapper.selectSystemTypeOnly(id);
        if (ObjectUtil.isNotNull(type) && type.getStatus() == 3) {
            //删除关联的武器库
            List<WeaponRelation> weaponRelations = weaponHouseMapper.selectWeaponTargetRelationByWeaponId(id);
            if (CollectionUtil.isNotEmpty(weaponRelations)) {
                for (WeaponRelation weaponRelation : weaponRelations) {
                    //清理分析中使用的数据
                    knowService.deleteSubTargetValue(weaponRelation.getId());
                }
                //最终删除关联数据
                weaponHouseMapper.delWeaponTargetRelationByWeaponId(id);
            }
            //清理关联数据
            weaponHouseMapper.delWeaponMessageByRelationId(id);
        }
        systemTypeMapper.deleteSystemType(id);
        return ResultUtil.success("删除类型成功", null);
    }

    @Override
    public Result selectSystemType(int pageNum, int pageSize, Integer status) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(pageNum, pageSize);
        List<SystemType> list = systemTypeMapper.selectSystemTypeList(status);
        PageInfo<SystemType> pageInfo = new PageInfo<>(list);
        List<HashMap<String, Object>> lists = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(pageInfo.getList())) {
            for (SystemType systemType : pageInfo.getList()) {
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("id", systemType.getId());
                if (status == 2) {
                    hashMap.put("name", systemType.getTypeId());
                    hashMap.put("typeId", systemType.getTypeName());
                } else if (status == 3) {
                    hashMap.put("name", systemType.getTypeName());
                    hashMap.put("typeId", systemType.getTypeId());
                } else {
                    TargetType type = targetTypeMapper.selectTypeOnly(systemType.getTypeId());
                    if (null != type && StringUtils.isNotBlank(type.getTypeName())) {
                        hashMap.put("name", type.getTypeName());
                    } else {
                        hashMap.put("name", "当前类型已被删除，请重新选择");
                    }
                    hashMap.put("typeId", systemType.getTypeId());
                }
                lists.add(hashMap);
            }
        }
        resultMap.put("typeList", lists);
        resultMap.put("pageNum", pageInfo.getPageNum());
        resultMap.put("pageSize", pageInfo.getPageSize());
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("pageAll", pageInfo.getPages());
        return ResultUtil.success("查询成功", resultMap);
    }

    @Override
    public Result selectSystemTypePut(Integer status) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<SystemType> systemTypes = systemTypeMapper.selectSystemTypeList(status);
        if (CollectionUtil.isNotEmpty(systemTypes)) {
            for (SystemType systemType : systemTypes) {
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("value", systemType.getId());
                if (status == 2) {
                    hashMap.put("label", systemType.getTypeId());
                } else {
                    TargetType type = targetTypeMapper.selectTypeOnly(systemType.getTypeId());
                    if (null != type && StringUtils.isNotBlank(type.getTypeName())) {
                        hashMap.put("label", type.getTypeName());
                    } else {
                        hashMap.put("label", "当前类型已被删除，请重新选择");
                    }
                }
                resultList.add(hashMap);
            }
        }
        return ResultUtil.success(resultList);
    }
}